package cn.school.workflow.service.impl;

import cn.school.classManage.dao.SchoolClassDao;
import cn.school.classManage.vo.SchoolClassVO;
import cn.school.common.utils.SysUserUtils;
import cn.school.student.entity.StudentInfo;
import cn.school.sys.dao.SysDeptDao;
import cn.school.sys.entity.SysDept;
import cn.school.sys.entity.SysDictDetail;
import cn.school.sys.entity.SysUser;
import cn.school.sys.service.SysDictDetailService;
import cn.school.workflow.dao.StudentLeaveApplyDao;
import cn.school.workflow.service.StudentLeaveStatisticsService;
import cn.school.workflow.vo.StudentLeaveApplyVO;
import cn.school.workflow.vo.StudentLeaveStatisticsVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
@Service
public class studentLeaveStatisticsServiceImpl implements StudentLeaveStatisticsService {
    @Autowired
    StudentLeaveApplyDao studentLeaveApplyDao;
    @Autowired
    SchoolClassDao schoolClassDao;
    @Autowired
    SysDictDetailService sysDictDetailService;

    @Override
    public IPage<StudentLeaveStatisticsVo> findStudentLeaveApplyPageList(StudentLeaveStatisticsVo vo) {
        LambdaQueryWrapper<StudentLeaveApplyVO> wrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getClassName())){
            wrapper.like(StudentLeaveApplyVO::getClassName,vo.getClassName());
        }
        if(!StringUtils.isEmpty(vo.getEndTime())){
            wrapper.le(StudentLeaveApplyVO::getModifyTime,vo.getEndTime());
        }
        if(!StringUtils.isEmpty(vo.getStartTime())){
            wrapper.ge(StudentLeaveApplyVO::getModifyTime,vo.getStartTime());
        }

        //只查询审批通过的
        wrapper.eq(StudentLeaveApplyVO::getStatus,"3");
        List<StudentLeaveApplyVO> studentLeaveApplyVOS = studentLeaveApplyDao.selectList(wrapper);
        //计算总条数
        wrapper.groupBy(StudentLeaveApplyVO::getClassId);
        int total = studentLeaveApplyDao.selectList(wrapper).size();
        int num = 0;
        //封装统计表
        List<StudentLeaveStatisticsVo> statisticsVoList = new ArrayList<>();
        //查询所有部门
        List<SysDictDetail> dictType = sysDictDetailService.selectDictDataByType("class_dept");

        for(StudentLeaveApplyVO slaVo:studentLeaveApplyVOS){
            if(slaVo.getClassName()==null){continue;}
            //判断班级名是否重名
            boolean chack = false;
            for(StudentLeaveStatisticsVo leaClassName:statisticsVoList){
                if(slaVo.getClassName().equals(leaClassName.getClassName())){
                    leaClassName.setLeaveNum(leaClassName.getLeaveNum()+1L);
                    chack = true;
                }
            }
            if (chack){continue;}
            StudentLeaveStatisticsVo slsVo = new StudentLeaveStatisticsVo();
            slsVo.setClassName(slaVo.getClassName());
            slsVo.setClassId(slaVo.getClassId());
            //获取对应班级部门
            SchoolClassVO schoolClassVO = schoolClassDao.selectById(slaVo.getClassId());
            //判断部门是否为空
            for(SysDictDetail sysDictDetail:dictType){
                if(schoolClassVO==null){
                    slsVo.setDepartment("暂无部门");
                    break;
                }else {
                    int classDept = Integer.parseInt(schoolClassVO.getClassDept());
                    if(sysDictDetail.getDictSort()==classDept){
                        slsVo.setDepartment(sysDictDetail.getDictLabel());
                        break;
                    }
                }
            };

            slsVo.setLeaveNum(1L);
            num++;
            statisticsVoList.add(slsVo);

            if(num>vo.getPageSize()){
                num = 0;
                break;
            }
        }

        IPage<StudentLeaveStatisticsVo> page = new Page<>();
        page.setRecords(statisticsVoList);
        page.setTotal(total);
        page.setSize(vo.getPageSize());
        page.setPages(vo.getPageNum());
        return page;
    }

    @Override
    public IPage<StudentLeaveStatisticsVo> pagelist(StudentLeaveStatisticsVo vo) {
        return null;
    }
}
